package com.geeklan.smart.data.remote;

import com.geeklan.smart.utils.SharedPreferencesUtil;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
public class IpApiClient {

    private static final String BASE_URL = "https://api.ipdatacloud.com/v2/query";
    private static final String API_KEY = "e87ef98ee04311efac5100163e167ffb";

    private final OkHttpClient client;

    public IpApiClient() {
        client = new OkHttpClient();
    }

    /**
     * 异步请求并提取需要的数据
     */
    public void getIpDataAsync() {
        String url = BASE_URL + "?key=" + API_KEY;

        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful() && response.body() != null) {
                    String responseBody = response.body().string();
                    parseAndPrintData(responseBody);
                } else {
                    System.out.println("响应失败: " + response.code());
                }
            }
        });
    }

    /**
     * 同步请求并提取需要的数据
     */
    public void getIpDataSync() {
        String url = BASE_URL + "?key=" + API_KEY;

        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful() && response.body() != null) {
                String responseBody = response.body().string();
                parseAndPrintData(responseBody);
            } else {
                System.out.println("响应失败: " + response.code());
            }
        } catch (IOException e) {
            System.out.println("请求失败: " + e.getMessage());
        }
    }

    /**
     * 解析 JSON 数据并提取 city, province, 和 ip
     *
     * @param jsonResponse JSON 响应字符串
     */
    private void parseAndPrintData(String jsonResponse) {
        try {
            JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
            JsonObject data = jsonObject.getAsJsonObject("data");
            JsonObject location = data.getAsJsonObject("location");

            String city = location.get("city").getAsString();
            String province = location.get("province").getAsString();
            String ip = location.get("ip").getAsString();

            SharedPreferencesUtil.putString("address_city",city);
            SharedPreferencesUtil.putString("address_province",province);
            SharedPreferencesUtil.putString("address_ip","0.0.0.0".equals(ip) ? "119.123.207.198" : ip);

            System.out.println("城市: " + city);
            System.out.println("省份: " + province);
            System.out.println("IP 地址: " + ip);
        } catch (Exception e) {
            System.out.println("解析 JSON 数据失败: " + e.getMessage());
        }
    }
}
